<template>
  <!-- 我的待办 -->
  <div class="module" style="min-height: 265px;max-height: 400px;">
    <a-tabs default-active-key="1" >
      <a-tab-pane key="1">
        <template slot="tab">
          {{$t('m.system.portlet.mytask.myTodoTask')}}
          <span>({{todoCount}})</span>
        </template>
        <div class="module-in">
          <div class="backlog">
            <a-table
              rowKey="id"
              :columns="todoColumns"
              :data-source="todo"
              :show-header="false"
              :pagination="false"
              size="small"
            >
              <template slot="links" slot-scope="text, record">
                <a @click="openTask(record)">{{text}}</a>
              </template>
            </a-table>
          </div>
        </div>
      </a-tab-pane>
      <a-tab-pane key="2" force-render>
        <template slot="tab">
          {{$t('m.system.portlet.mytask.myReadTask')}}
          <span>({{toreadCount}})</span>
        </template>
        <div class="module-in">
          <div class="backlog">
            <a-table
              rowKey="id"
              :columns="todoColumns"
              :data-source="toread"
              :show-header="false"
              :pagination="false"
              size="small"
            >
              <template slot="links" slot-scope="text, record">
                <a @click="openTask(record)">{{text}}</a>
              </template>
            </a-table>
          </div>
        </div>
      </a-tab-pane>
      <!-- 右侧图标 -->
      <template slot="tabBarExtraContent">
        <ych-icon type="sync" class="panel-icon" @click="loadData"/>
        <ych-icon type="more" class="panel-icon" @click="loadMore"/>
      </template>
    </a-tabs>
    <redirect-modal ref="flowForm" @refreshParent="modalFormOk"></redirect-modal>
  </div>
</template>
<script>

  import { getAction} from '@/api/manage'
  import RedirectModal from '../../bpm/redirect/RedirectModal';
  import YchConstant from "@/api/YchConstant";

  // 我的待办table
  export default {
    name: 'WaitTodo',
    components: {
      RedirectModal
    },
    data: () => {
      return {
        loading: false,
        url:{
          queryTodoAndToRead:YchConstant.PREFIX_BPM + "/bpm-front/taskRest/queryTodoAndToRead/v1"
        },
        todo:[], //我的待办
        todoCount:0,
        toread:[],//我的待阅
        toreadCount:0,
        todoColumns:[
          {
            title:  '任务标题',
            dataIndex: 'taskTitle',
            ellipsis: 'true', //设置溢出变省略号
            scopedSlots: { customRender: 'links' },
          },
          {
            title:  '发送人',
            dataIndex: 'fromUserName',
            width: '120px',
            align: 'center',
            ellipsis: 'true',
            className: 'color01'
          },
          {
            title: '接收时间',
            dataIndex: 'sendTimeString',
            width: '120px',
            align: 'center',
            ellipsis: 'true',
            className: 'color01'
          }
        ],
      };
    },
    created () {
      this.loadData()
    },
    inject: ['addRouter'],
    methods: {
      loadData(){
        this.loading = true
        let that = this
        let param = {
          limit:8
        }
        getAction(this.url.queryTodoAndToRead, param).then(res => {
          if (res.success) {
            that.todo = res.result.todo
            that.todoCount = res.result.todoCount
            that.toread = res.result.toread
            that.toreadCount = res.result.toreadCount
          } else {
            this.messageUtil.warning(res.message)
          }
          this.loading = false
        })
      },
      openTask(record){
        if (record.pcUrl.startsWith("router:")) {
          let url = record.pcUrl.substr("router:".length);
          if(url.indexOf("?") === -1){
            url += "?taskId=" + record.id + "&finish=0"
          } else {
            url += "&taskId=" + record.id + "&finish=0"
          }
          this.$router.push(url).catch(err => {})
          return
        } else if (record.pcUrl.startsWith("http:") || record.pcUrl.startsWith("https:")) {
          // http 开头的链接，暂时以新页签形式打开
          let url = record.pcUrl;
          if (url.indexOf("?") === -1) {
            url += "?taskId=" + record.id + "&finish=0"
          } else {
            url += "&taskId=" + record.id + "&finish=0"
          }
          window.open(url)
          return
        }
        let router = null
        if (this.$store.getters.formOpenType === '2') {
          router = this.addRouter({
            path: '/bpm/detailFromTask/' + record.id,
            component: resolve => require(['@/views/bpm/redirect/RedirectTab.vue'], resolve),
            props: {
              id: record.id,
              func: 'detailFromTask',
            },
            meta: {
              title: this.$t('m.system.portlet.mytask.detailFromTask'),
            }
          });
          if (router) {
            this.$router.push(router).catch(err => {})
          }
        }
        if (!router) {
          this.$refs.flowForm.detailFromTask(record.id)
        }
      },
      modalFormOk(){
        this.loadData()
      },
      loadMore(){
        this.$router.push({
          path: '/process/task'
        }).catch(err => {})
      },
    },
  };
</script>

<style lang="less">
  @import '~@/views/system/portlet/css/basicStyle.less';
</style>
